package Demo02;


import java.util.*;

class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Animal animal = (Animal) o;
        return Objects.equals(name, animal.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
public class Test {
    public static int singleNumber(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])) {
                set.remove(nums[i]);
            }else {
                set.add(nums[i]);
            }
        }
        Object[] arr = set.toArray();
        return (int)arr[0];
    }

    public static int numJewelsInStones(String jewels, String stones) {
        HashMap<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < stones.length(); i++) {
            Character c = stones.charAt(i);
            if(!map.containsKey(c)) {
                map.put(c,1);
            }else {
                map.put(c, map.get(c)+1);
            }
        }
        int count = 0;
        for (int i = 0; i < jewels.length(); i++) {
            if(map.containsKey(jewels.charAt(i))) {
                count += map.get(jewels.charAt(i));
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        HashSet<Character> set = new HashSet<>();
        for (Character c :str2.toUpperCase().toCharArray()) {
            set.add(c);
        }
        HashSet set1 = new HashSet();
        for (Character c :str1.toUpperCase().toCharArray()) {
            if(!set.contains(c)&&!set1.contains(c)) {
                System.out.print(c);
                set1.add(c);
            }
        }
    }
    public static void main01(String[] args) {
/*        int[] arr = {2,2,1};
        System.out.println(singleNumber(arr));*/
        String a2 = "aAAbbbb";
        String a1 = "aA";
        System.out.println(numJewelsInStones(a1, a2));
    }
}


class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}


class Solution {
    public Node copyRandomList(Node head) {
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        while (cur!=null) {
            Node newNode = new Node(cur.val);
            map.put(cur,newNode);
            cur = cur.next;
        }
        cur = head;
        while (cur!=null) {
            Node tmpNode1 = map.get(cur.next);
            Node tmpNode2 = map.get(cur.random);
            Node newNode = map.get(cur);
            newNode.next = tmpNode1;
            newNode.random = tmpNode2;
            cur = cur.next;
        }
        return map.get(head);
    }
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        for ( String str : words) {
            if(map.get(str)==null) {
                map.put(str,1);
            }else {
                int num = map.get(str);
                map.put(str,num+1);
            }
        }
        PriorityQueue<Map.Entry<String,Integer>> p = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue())==0) {
                    return o2.getValue().compareTo(o1.getValue());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        for (Map.Entry<String,Integer> x : map.entrySet()) {
            if(k > p.size()) {
                p.add(x);
            }else {
                if(x.getValue()>p.peek().getValue()) {
                    p.poll();
                    p.offer(x);
                } else {
                    if(x.getValue().compareTo(p.peek().getValue())==0) {
                        if(p.peek().getKey().compareTo(x.getKey())>0) {
                            p.poll();
                            p.offer(x);
                        }
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            Map.Entry<String,Integer> top = p.poll();
            ret.add(top.getKey());
        }
        Collections.reverse(ret);
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] s = {"i","love","leetcode","i","love","coding"};
        System.out.println(solution.topKFrequent(s, 2));
    }
}
